<?php 
namespace app\common\lib;

class Common
{
	/**
     * 请求远程接口
     * @return [type] [description]
     */
    public function requestInterface(  $url ,$param )
    {   
        
        $param = http_build_query($param);

        $result = $this->_crul_post($url,$param);

        $res = json_decode( $result , true );
        if( empty( $res ) ){
            throw new \Exception("异常:未知错误,code:".'50000'.$result);
        }
        return $res;
    }


     /**
     * CURL方式的POST传值
     * @param  [type] $url  [POST传值的URL]
     * @param  [type] $data [POST传值的参数]
     * @return [type]       [description]
     */
    public function _crul_post($url,$data){
        //初始化curl       
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url); 
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        //post提交方式
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        //要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //运行curl
        $result = curl_exec($curl);
        //返回结果      
        if (curl_errno($curl)) {
           return 'Errno'.curl_error($curl);
        }
        curl_close($curl);
      
        return $result;
    }
}
